package com.woniu.secondhand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.secondhand.constant.SecondhandCategoryConstant;
import com.woniu.secondhand.entity.vo.CategoryIndexVO;
import com.woniu.secondhand.mapper.SecondhandCategoryMapper;
import com.woniu.secondhand.pojo.SecondhandCategory;
import com.woniu.secondhand.service.ISecondhandCategoryService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HMMT
 * @since 2023-05-20
 */
@Service
public class SecondhandCategoryServiceImpl extends ServiceImpl<SecondhandCategoryMapper, SecondhandCategory> implements ISecondhandCategoryService {
    @Autowired
    private SecondhandCategoryMapper categoryMapper;

    @Override
    @Cacheable(value = SecondhandCategoryConstant.CATEGORY_VALUE, key = SecondhandCategoryConstant.CATEGORY_KEY)
    public List<CategoryIndexVO> listAllFirst() {
        LambdaQueryWrapper<SecondhandCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondhandCategory::getLevel, SecondhandCategoryConstant.FIRST_LEVEL);
        List<SecondhandCategory> secondhandCategories = categoryMapper.selectList(wrapper);
        CategoryIndexVO categoryIndexVO = new CategoryIndexVO();
        List<CategoryIndexVO> categoryIndexVOList = new ArrayList<>();
        for (SecondhandCategory c : secondhandCategories) {
            BeanUtils.copyProperties(c, categoryIndexVO);
            categoryIndexVOList.add(categoryIndexVO);
            categoryIndexVO = new CategoryIndexVO();
        }
        return categoryIndexVOList;
    }

    @Override
    public CategoryIndexVO getCategoryByParentId(Integer parentId) {
        //搜索一级分类
        QueryWrapper<SecondhandCategory> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("id", parentId);
        categoryQueryWrapper.eq("level", SecondhandCategoryConstant.FIRST_LEVEL);
        SecondhandCategory firstCategory = categoryMapper.selectOne(categoryQueryWrapper);
        if (firstCategory == null) {
            return null;
        }
        CategoryIndexVO firstCategoryVO = new CategoryIndexVO();
        BeanUtils.copyProperties(firstCategory, firstCategoryVO);
        //搜索一级分类下的次级分类
        List<CategoryIndexVO> secondCategoryIndexVOList = listByParentId(parentId);
        firstCategoryVO.setChildren(secondCategoryIndexVOList);
        if (secondCategoryIndexVOList.size() > 0) {
            for (CategoryIndexVO secondCategory : secondCategoryIndexVOList) {
                List<CategoryIndexVO> thirdCategoryList = listByParentId(secondCategory.getId());
                secondCategory.setChildren(thirdCategoryList);
            }
        }
        return firstCategoryVO;
    }

    @NotNull
    private List<CategoryIndexVO> listByParentId(Integer parentId) {
        LambdaQueryWrapper<SecondhandCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondhandCategory::getParentId, parentId);
        List<SecondhandCategory> secondhandCategories = categoryMapper.selectList(wrapper);
        List<CategoryIndexVO> secondCategoryIndexVOList = new ArrayList<>();
        CategoryIndexVO categoryIndexVO = new CategoryIndexVO();
        for (SecondhandCategory c : secondhandCategories) {
            BeanUtils.copyProperties(c, categoryIndexVO);
            secondCategoryIndexVOList.add(categoryIndexVO);
            categoryIndexVO = new CategoryIndexVO();
        }
        return secondCategoryIndexVOList;
    }
}
